import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class _2设置索引库使用的分词器IK {

    //定义全局变量
   private  PreBuiltTransportClient transportClient;


    /*配置初始化*/
    @Before
    public void Before() throws UnknownHostException {

        //1.创建一个settings对象,xiang相当于是一个配置信息,主要配置集群的名字
          //Settings settings= Settings.builder().put("cluster.name","cluster-es").build();
            Settings settings = Settings.EMPTY;// 这个是单节点创建,等同于上面的的代码创建
        //2.创建一个客户端对象
         transportClient = new PreBuiltTransportClient(settings);
        //3.设置单节点/集群的信息(java和es引擎端口之间的连接是9300)
         transportClient.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"),9300));

    }


    /*"mappings": {
        "content": {
        "properties": {
        "id": {
        "type": "text",
        "fields": {
        "keyword": {
        "ignore_above": 256,
        "type": "keyword"
        }
        }
        },
        "title": {
        "type": "text",
        "fields": {
        "keyword": {
        "ignore_above": 256,
        "type": "keyword"
        }
        }
        },
        "content": {
        "type": "text",
        "fields": {
        "keyword": {
        "ignore_above": 256,
        "type": "keyword"
        }
        }
        }
        }
        }
        },*/

    /*给索引库设置分词器  拼接如上的信息*/
    @Test
     public void createMapping() throws IOException {

        // 创建索引库
        // CreateIndexResponse response= transportClient.admin().indices().prepareCreate("blog1").get();

        //1.建立json规则
        XContentBuilder contentBuilder= XContentFactory.jsonBuilder().startObject()
        .startObject("content")
        .startObject("properties")

        //设置id相关的属性类型和要存储
        .startObject("id").field("type","long").field("store",true).endObject()
        //配置标题的属性类型和存储和配分词器 (细粒度切分使用 ik_max_word 最小切分 ik_smart)
        .startObject("title").field("type","text").field("store",true).field("analyzer","ik_smart").endObject()
        .startObject("content").field("type","text").field("store",true).field("analyzer","ik_smart").endObject()

        .endObject()
        .endObject()
        .endObject();

        //2.执行规则 注意:执行规则的时候需要提前创建索引库,执行规则不会创建索引库
        transportClient.admin().indices()
            //设置要做映射的索引库名
            .preparePutMapping("blog1")
            //设置映射的类型
            .setType("content")
            //配置规则和索引库的关联
            .setSource(contentBuilder)
            .get();


        //关闭资源
        transportClient.close();
     }



}



